package com.hy.dao;

import cn.hutool.core.io.FileUtil;
import cn.hutool.log.Log;
import com.hy.bean.Card;
import com.hy.bean.Follow;
import com.hy.bean.User;
import com.hy.utils.DBManager;
import com.hy.utils.DateUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserDao {
    private QueryRunner qr = new QueryRunner();

    public List<User> selectUser(String username) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select * from ums_user where user_name = ?";
        BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
        List<User> query = qr.query(conn, sql, new BeanListHandler<>(User.class,brp),username);
        DbUtils.close(conn);
        return query;
    }

    public User selecUserById(Integer userId){
        User user = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user where id = ?";
            BasicRowProcessor rowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            user = qr.query(conn,sql,new BeanHandler<>(User.class,rowProcessor),userId);
            DbUtils.close(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public boolean updateUser(User user) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "UPDATE `ums_user` SET `user_name` = ?, `img` = ?, `sex` = ?, `introduce` = ?, `residence` = ?, `vocation` = ?, `professional_history` = ?, `education_experience` = ?, `individual_resume` = ?, `update_time` = ?, `update_id` = ? WHERE `id` = ?;";
        int update = qr.update(conn, sql, user.getUserName(),user.getImg(),user.getSex(),user.getIntroduce(),user.getResidence(),user.getVocation(),user.getProfessionalHistory(),user.getEducationExperience(),user.getIndividualResume(),user.getUpdateTime(),user.getUpdateId(),user.getId());
        DbUtils.close(conn);
        return update>0?true:false;
    }

    public User selectById(String userName){
        User user = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user where tombstone = '0' and user_name = ?";
            BasicRowProcessor rowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            user = qr.query(conn,sql,new BeanHandler<>(User.class,rowProcessor),userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

//    查看我关注的人数
    public Long selectMyInterest(Integer userId){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select count(user1_id) from follow where user_id = ? and tombstone = '0'";
            count = qr.query(conn, sql, new ScalarHandler<>(), userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? count : 0;
    }

//    查看关注者的人数
    public Long selectMyNoticer(Integer userId){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select count(user_id) from follow where user1_id = ? and tombstone = '0'";
            count = qr.query(conn, sql, new ScalarHandler<>(), userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? count : 0;
    }

//    我关注的用户信息
    public List<Map<String,Object>> selectMyInterestUser(Integer userId){
        List<Map<String,Object>> list = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user where id in (select user1_id from follow where user_id = ? and tombstone = '0')";
            list = qr.query(conn, sql, new MapListHandler(), userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    //查询关注我的用户信息
    public List<Map<String,Object>> selectFollowMeUser(Integer userId){
        List<Map<String,Object>> list = null;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select * from ums_user where id in (select user_id from follow where user1_id = ? and tombstone = '0')";
            list = qr.query(conn, sql, new MapListHandler(), userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

//    统计用户发布的文章数量
    public Long selectCardByUserId(Integer userId){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select count(*) from card where user_id = ? and tombstone = '0'";
            count = qr.query(conn, sql, new ScalarHandler<>(), userId);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? count : 0;
    }

//    取消关注、关注
    public boolean updateFollowTombstone(Follow follow){
        int count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "update follow set tombstone = ?,update_time = ?,update_id = ? where user_id = ? and user1_id = ?";
            count = qr.update(conn,sql,follow.getTombstone(),follow.getUpdateTime(),follow.getUpdateId(),follow.getUserId(),follow.getUser1Id());
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? true : false;
    }

//    是否相互关注
    public boolean selectWhetherInterest(Integer userId,Integer user1Id){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select count(1) from follow where user_id = ? and user1_id = ? and tombstone = '0'";
            count = qr.query(conn,sql,new ScalarHandler<>(),userId,user1Id);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? true : false;
    }

    public boolean selectWhetherInterest1(Integer userId,Integer user1Id){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "select count(1) from follow where user_id = ? and user1_id = ?";
            count = qr.query(conn,sql,new ScalarHandler<>(),userId,user1Id);
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? true : false;
    }

//    添加关注
    public boolean insertFollow(Follow follow){
        long count = 0;
        try {
            Connection conn = DBManager.getConn();
            String sql = "INSERT INTO `follow` (`user_id`, `user1_id`, `create_time`, `create_id`,  `tombstone`) VALUES (?, ?, ?, ?, ?);";
            count = qr.update(conn,sql,follow.getUserId(),follow.getUser1Id(),follow.getCreateTime(),follow.getCreateId(),follow.getTombstone());
            DbUtils.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count > 0 ? true : false;
    }
    public static void main(String[] args) {
        System.out.println(new UserDao().selectWhetherInterest(2,1));
    }
}
